package com.dinggou.shopjb.activitys;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.dinggou.shopjb.R;
import com.dinggou.shopjb.adapters.EXPRecordAdapter;
import com.dinggou.shopjb.application.MyApplication;
import com.dinggou.shopjb.base.BaseActivity;
import com.dinggou.shopjb.bean.EXPRecordBean;
import com.dinggou.shopjb.net.ActionCallbackListener;
import com.dinggou.shopjb.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class EXPRecordActivity extends BaseActivity {

    private SmartRefreshLayout refresh_layout;
    private RecyclerView rv_ex_record;
    private EXPRecordAdapter adapter;

    private List list = new ArrayList<>();
    private  int page = 1;
    @Override
    protected int setContentView() {
        return R.layout.activity_exp_record;
    }

    @Override
    protected void findViews() {
        refresh_layout = $(R.id.refresh_layout);
        rv_ex_record = $(R.id.rv_ex_record);

        refresh_layout = $(R.id.refresh_layout);
        refresh_layout.setRefreshFooter(new ClassicsFooter(mContext).setSpinnerStyle(SpinnerStyle.Translate));
        rv_ex_record.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new EXPRecordAdapter(mContext,list);
        rv_ex_record.setAdapter(adapter);
        getListDate();
    }

    @Override
    protected void setListeners() {
        refresh_layout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getListDate();
            }
        });

        refresh_layout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getListDate();
            }
        });
    }
    private void getListDate(){
        refresh_layout.finishRefresh(2000);
        refresh_layout.finishLoadMore(2000);
        mAppAction =  MyApplication.getApplication().getAppAction();

        mAppAction.getExperienceLog(page, new ActionCallbackListener<List<EXPRecordBean>>() {
        @Override
        public void onSuccess(List<EXPRecordBean> data) {
                if (page ==1){
                    list.clear();
                }
                list.addAll(data);
                adapter.notifyDataSetChanged();

        }

        @Override
        public void onFailure(String errorCode, String message) {
            ToastUtil.showToast(mContext,message);
        }
    });
    }
}
